#include "main_app.h"
#include "esp_sntp.h"
#include "http_flie_server.h"
#include "time_sntp_driver.h"
#include "camera_driver.h"
#include "fatfs_driver.h"
#include "szp_hardware_driver.h"
#include "web_server_driver.h"
#include "my_ui_api.h"
#include "actions.h"
#include "ui.h"
#include "screens.h"
#include "vars.h"
#include "fonts.h"
#include "styles.h"

static const char *TAG = "main_app";
static const char *file_photo_quality_config = MOUNT_POINT "/config/photo_quality_config";
static const char *file_photo_pixel_config = MOUNT_POINT "/config/photo_pixel_config";

extern TaskHandle_t task_camera_get_handle, task_camera_show_handle, task_take_photo_handle;

void picture_select_cb(lv_event_t *e)
{
    const char *file_name = (const char *)lv_event_get_user_data(e);
    ESP_LOGI(TAG, "click = %s", file_name);
    // eez_flow_set_screen(SCREEN_ID_PICTURE_SHOW_NOW, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0);
    // char path[20];
    // lv_label_set_text(objects.fille_name_label, file_name);
    // sprintf(path, "S:/%s", file_name);
    // lv_img_set_src(objects.picture_show_img, path);
    show_picture(file_name);
}

void task_time_frush(void *arg)
{
    char time_str[32];
    char *task_list_buffer = malloc(1024);
    while (1)
    {
        get_time_from_sys(time_str);
        set_var_now_time_str(time_str);
        vTaskDelay(pdMS_TO_TICKS(1000));

        // vTaskList(task_list_buffer);
        // printf("======== Task States ========\n");
        // printf("%s", task_list_buffer);
        // printf("\nFreeHeapSize:%d\n", xPortGetFreeHeapSize());
    }
}

void main_task_start(void)
{
    char buf[10];
    struct stat st;

    ESP_LOGI(TAG, "get_var_select_pixel() = %d", get_var_select_pixel());
    ESP_LOGI(TAG, "get_var_photo_quality() = %d", get_var_photo_quality());
    // fatfs_init();
    set_time_from_file();

    if (stat(MOUNT_POINT "/config", &st) == 0 && S_ISDIR(st.st_mode))
    {
        ESP_LOGI(TAG, "config dir already exist");
    }
    else
    {
        ESP_LOGI(TAG, "config dir not exist, create it");
        mkdir(MOUNT_POINT "/config", 0777);
    }

    if (fatfs_read_file(file_photo_quality_config, buf) != ESP_OK)
    {
        ESP_ERROR_CHECK(fatfs_write_file(file_photo_quality_config, itoa(get_var_photo_quality(), buf, 10)));
    }
    int photo_quality = (int)(63 - (float)atoi(buf) / 100 * 63);
    set_var_photo_quality(atoi(buf));

    if (fatfs_read_file(file_photo_pixel_config, buf) != ESP_OK)
    {
        ESP_ERROR_CHECK(fatfs_write_file(file_photo_pixel_config, itoa(get_var_select_pixel(), buf, 10)));
    }
    framesize_t photo_pixel;
    set_var_select_pixel(atoi(buf));

    switch (atoi(buf))
    {
    case 0:
        photo_pixel = FRAMESIZE_QVGA;
        break;

    case 1:
        photo_pixel = FRAMESIZE_QQVGA;
        break;

    case 2:
        photo_pixel = FRAMESIZE_VGA;
        break;

    default:
        photo_pixel = FRAMESIZE_QVGA;
        break;
    }
    ESP_LOGI(TAG, "success get photo config, quality=%d,pixel=%d", photo_quality, photo_pixel);

    camera_init(photo_quality, photo_pixel);
    buttton_init();

    xTaskCreatePinnedToCore(task_take_photo, "task_take_photo", 5 * 1024, NULL, 5, &task_take_photo_handle, 1);
    xTaskCreatePinnedToCore(task_camera_get, "task_camera_get", 6 * 1024, NULL, 5, &task_camera_get_handle, 1);
    xTaskCreatePinnedToCore(task_camera_show, "task_camera_show", 6 * 1024, NULL, 5, &task_camera_show_handle, 0);
    xTaskCreatePinnedToCore(task_time_frush, "task_time_frush", 5 * 1024, NULL, 5, NULL, 0);
}
